const dbconfig = require("../../util/dbconfig")
// 增
const addData = (req, res) => {
  const sql = `insert into test set name=?, phone=?, sex=?`
  console.log(req.body);
  const sqlArr = [req.body.name, req.body.phone, req.body.sex]
  const callBack = (err, data) => {
    if (err) {
      res.send({
        'code': 404,
        'msg': '新增数据失败',
        'err': err
      })
      console.log('新增数据失败')
    } else {
      res.send({
        'code': 200,
        'msg': '新增数据成功'
      })
      console.log('新增数据成功')
    }
  }
  dbconfig.sqlConnect(sql, sqlArr, callBack)
}
// 删
const deleteData = (req, res) => {
  const sql = 'delete from test where id=?'
  const sqlArr = [req.query.id]
  const callBack = (err, data) => {
    if (err) {
      res.send({
        'code': 404,
        'msg': '删除数据失败',
        'err': err
      })
      console.log('删除数据失败');
    } else {
      res.send({
        'code': 200,
        'msg': '删除数据成功'
      })
      console.log('删除数据成功')
    }
  }
  dbconfig.sqlConnect(sql, sqlArr, callBack)
}
// 改
const updatedData = (req, res) => {
  const sql = 'update test set phone=? where id=?'
  const sqlArr = [req.query.phone, req.query.id];
  const callBack = (err, data) => {
    if (err) {
      res.send({
        'code': 404,
        'msg': '更改数据失败',
        'err': err
      })
      console.log('更改数据失败')
    } else {
      res.send({
        'code': 200,
        'msg': '更改数据成功'
      })
      console.log('更改数据成功')
    }
  }
  dbconfig.sqlConnect(sql, sqlArr, callBack)
}
// 查
const selectData = (req, res) => {
  const sql = 'select * from  test'
  const sqlArr = []
  const callBack = (err, data) => {
    if (err) {
      res.send({
        'code': 404,
        'msg': '查询失败',
        'err': err
      })
      console.log('查询失败')
    } else {
      res.send({
        'code': 200,
        'msg': '查询数据成功',
        data
      })
      console.log('查询成功')
    }
  }
  dbconfig.sqlConnect(sql, sqlArr, callBack)
}
module.exports = {
  addData, deleteData, updatedData, selectData
}